#include "lvglvirtualscreen.h"
#include <QDebug>
#include <QApplication>
LvglVirtualScreen *thisAppWindow = nullptr;
LvglVirtualScreen::LvglVirtualScreen(int w, int h, uchar *ram, QWidget *parent)
    : QWidget(parent)
{
    thisAppWindow = this;
    mousePressFlag = false;
    virtualScreenH = h;
    virtualScreenW = w;
    if(ram != nullptr)
    {
        vRam = ram;
    }
    else
    {
        vRam = new uchar[w * h * 4];
    }

    workThread = new LvglThread();
    workThread->start();
    setFixedSize(virtualScreenW, virtualScreenH);
}

LvglVirtualScreen::~LvglVirtualScreen()
{
}

void LvglVirtualScreen::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawImage(this->rect(), QImage(vRam, virtualScreenW, virtualScreenH, QImage::Format_RGB16));
    event->accept();
}

uchar *LvglVirtualScreen::getVRam()
{
    return vRam;
}


void LvglVirtualScreen::mousePressEvent(QMouseEvent *event)
{
    mousePressFlag = true;
    x = event->pos().rx();
    y = event->pos().ry();
    event->accept();
}

void LvglVirtualScreen::mouseReleaseEvent(QMouseEvent *event)
{
    mousePressFlag = false;
    x = event->pos().rx();
    y = event->pos().ry();
    event->accept();
}

void LvglVirtualScreen::mouseMoveEvent(QMouseEvent *event)
{
    mousePressFlag = true;
    x = event->pos().rx();
    y = event->pos().ry();
    event->accept();
}

void LvglVirtualScreen::getMousePos(int16_t &x, int16_t &y)
{
    x = this->x;
    y = this->y;
}

bool LvglVirtualScreen::getMousePressFlag()
{
    return mousePressFlag;
}
